using SmartHRMS.CustomExceptions.Infrastructure;
using System;
using System.Text;
using SmartHRMS.Infrastructure;

namespace SmartHRMS.Model.Company
{
    public class CompanyAddress : BusinessBase<CompanyAddress>
    {
        public Guid AddressId { get; private set; }

        public  string Country { get; set; }

        public  string Street { get; set; }

        public string HouseNumber { get; set; }

        public  string City { get; set; }

        public  string State { get; set; }

        public  string PostCode { get; set; }

        public  string OfficePhone { get; set; }

        public  string Fax { get; set; }

        public  string Email { get; set; }

        public  string Website { get; set; }


        public CompanyAddress()
        {
            // Default Constructor required for NHibernate
        }

        public CompanyAddress(string street, string houseNumber, string city, string county, string state, string postCode,
                        string officePhone, string fax, string email, string website)
        {
            this.Street = street;
            this.HouseNumber = houseNumber;
            this.City = city;
            this.State = state;
            this.Country = county;
            this.PostCode = postCode;
            this.OfficePhone = officePhone;
            this.Fax = fax;
            this.Email = email;
            this.Website = website;
            validate();
        }

        public override void validate()
        {
            StringBuilder sbBrokenRules = new StringBuilder();
			
            if (string.IsNullOrEmpty(this.Street.Trim()))
            {
                sbBrokenRules.AppendLine("Street is a required value");
            }
            else
            {
                if (this.Street.Length > 50)
                {
                    sbBrokenRules.AppendLine("Street should be 50 chars or less");
                }
            }

            if (string.IsNullOrEmpty(this.HouseNumber.Trim()))
            {
                sbBrokenRules.AppendLine("House number is a required value");
            }
            else
            {
                if (this.HouseNumber.Length > 50)
                {
                    sbBrokenRules.AppendLine("House number should be 50 chars or less");
                }
            }


            if (string.IsNullOrEmpty(this.State.Trim()))
            {
                sbBrokenRules.AppendLine("State is a required value");
            }
            else
            {
                if (this.State.Length > 50)
                {
                    sbBrokenRules.AppendLine("State should be 50 chars or less");
                }
            }

			
            if (string.IsNullOrEmpty(this.City.Trim()))
            {
                sbBrokenRules.AppendLine("City is a required value");
            }
            else
            {
                if (this.City.Length > 50)
                {
                    sbBrokenRules.AppendLine("City should be 50 chars or less");
                }
            }
			
            if (string.IsNullOrEmpty(this.Country.Trim()))
            {
                sbBrokenRules.AppendLine("County is a required value");
            }
            else
            {
                if (this.Country.Length > 50)
                {
                    sbBrokenRules.AppendLine("County should be 50 chars or less");
                }
            }
			
            if (string.IsNullOrEmpty(this.PostCode.Trim()))
            {
                sbBrokenRules.AppendLine("PostCode is a required value");
            }
            else
            {
                if (this.PostCode.Length > 50)
                {
                    sbBrokenRules.AppendLine("PostCode should be 50 chars or less");
                }
            }

            if (!string.IsNullOrEmpty(this.OfficePhone.Trim()))
            {
                if (this.OfficePhone.Length > 15)
                {
                    sbBrokenRules.AppendLine("Office Phone should be 15 chars or less");
                }
                //additiona validation goes here
            }

            if (!string.IsNullOrEmpty(this.Fax.Trim()))
            {
                if (this.Fax.Length > 15)
                {
                    sbBrokenRules.AppendLine("Fax should be 15 chars or less");
                }
                //additiona validation goes here
            }

            if (!string.IsNullOrEmpty(this.Email.Trim()))
            {
                if (this.Email.Length > 250)
                {
                    sbBrokenRules.AppendLine("Email should be 250 chars or less");
                }
                //additiona validation goes here
            }

            if (!string.IsNullOrEmpty(this.Website.Trim()))
            {
                if (this.Website.Length > 250)
                {
                    sbBrokenRules.AppendLine("Website should be 250 chars or less");
                }
            }
			
            if (! string.IsNullOrEmpty(sbBrokenRules.ToString()))
            {
                throw (new InvalidAddressException(string.Format("Broken rules on Address, please correct the following errors:{0}{1}", Environment.NewLine, sbBrokenRules.ToString())));
            }
        }
    }
}
